﻿namespace MSSqlSchemaDoc.Provider.MicrosoftSqlServer.Resources
{
    using System;
    using System.Data.SqlClient;

    /// <summary>
    /// Handles SQL versioning.
    /// </summary>
    public sealed class QueryManager
    {
        /// <summary>
        /// The version of the SQL Server.
        /// </summary>
        private int version;

        /// <summary>
        /// Initializes a new instance of the QueryManager class.
        /// </summary>
        /// <param name="connection">An open connection.</param>
        public QueryManager(SqlConnection connection)
        {
            this.version = Convert.ToInt32(connection.ServerVersion.Substring(0, connection.ServerVersion.IndexOf(".")));

            if (this.version <= 8)
            {
                throw new NotImplementedException("Support for SQL Server 2000 and before has not yet been implemented.");
            }
        }

        /// <summary>
        /// Retrieves the query for foreign keys.
        /// </summary>
        /// <returns>The relevant query.</returns>
        public string ForeignKeys()
        {
            return QueriesSql2005.ForeignKeys;
        }

        /// <summary>
        /// Retrieves the query for full text catalogs.
        /// </summary>
        /// <returns>The relevant query.</returns>
        public string FullTextCatalogs()
        {
            return QueriesSql2005.FullTextCatalogs;
        }

        /// <summary>
        /// Retrieves the query for table indexes.
        /// </summary>
        /// <returns>The relevant query.</returns>
        public string TableIndexes()
        {
            if (this.version == 9)
            {
                return QueriesSql2005.TableIndexes;
            }
            else
            {
                return QueriesSql2008.TableIndexes;
            }
        }

        /// <summary>
        /// Retrieves the query for tables.
        /// </summary>
        /// <returns>The relevant query.</returns>
        public string Tables()
        {
            return QueriesSql2005.Tables;
        }

        /// <summary>
        /// Retrieves the query for triggers.
        /// </summary>
        /// <returns>The relevant query.</returns>
        public string Triggers()
        {
            return QueriesSql2005.Triggers;
        }

        /// <summary>
        /// Retrieves the query for stored procedures.
        /// </summary>
        /// <returns>The relevant query.</returns>
        public string StoredProcedures()
        {
            return QueriesSql2005.StoredProcedures;
        }

        /// <summary>
        /// Retrieves the query for synonyms.
        /// </summary>
        /// <returns>The relevant query.</returns>
        public string Synonyms()
        {
            return QueriesSql2005.Synonyms;
        }

        /// <summary>
        /// Retrieves the query for user defined functions.
        /// </summary>
        /// <returns>The relevant query.</returns>
        public string UserDefinedFunctions()
        {
            return QueriesSql2005.UserDefinedFunctions;
        }

        /// <summary>
        /// Retrieves the query for view indexes.
        /// </summary>
        /// <returns>The relevant query.</returns>
        public string ViewIndexes()
        {
            if (this.version == 9)
            {
                return QueriesSql2005.ViewIndexes;
            }
            else
            {
                return QueriesSql2008.ViewIndexes;
            }
        }

        /// <summary>
        /// Retrieves the query for user defined views.
        /// </summary>
        /// <returns>The relevant query.</returns>
        public string Views()
        {
            return QueriesSql2005.Views;
        }
    }
}
